<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form :form="form" layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="线路">
              <j-popup
                v-decorator="['routeNames']"
                :trigger-change="true"
                org-fields="id,route_name"
                dest-fields="routeIds,routeNames"
                code="select_route"
                :multi="true"
                @callback="popupCallback"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="仓库">
              <j-popup
                v-decorator="['storeName']"
                :trigger-change="true"
                :list-data.sync="listData"
                org-fields="id,store_name"
                dest-fields="storeId,storeName"
                code="store_info"
                @callback="popupCallback"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="单据状态">
              <j-dict-select-tag
                placeholder="请选择单据状态"
                v-model="queryParam.state"
                dictCode="shipment_goods_bill_status"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset1" icon="reload" style="margin-left: 8px">重置</a-button>
              <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a> -->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!--      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>-->
      <a-button
        type="primary"
        icon="download"
        @click="handleExportXlsBtn('出货单')"
        :disabled="selectedRowKeys.length ? false : true"
        >导出</a-button
      >
      <!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">-->
      <!--        <a-button type="primary" icon="import">导入</a-button>-->
      <!--      </a-upload>-->
      <!--      <a-dropdown v-if="selectedRowKeys.length > 0">-->
      <!--        <a-menu slot="overlay">-->
      <!--          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>-->
      <!--        </a-menu>-->
      <!--        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>-->
      <!--      </a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, type: 'radio', onChange: onSelectChange }"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <span v-show="record.state == 0" v-has="'shipmentgoods:approval'">
            <a @click="handleApp(record)">审核</a>
            <a-divider type="vertical" />
          </span>

          <a @click="handleDetail(record)">详情</a>
        </span>
      </a-table>
    </div>

    <shipment-goods-bill-modal ref="modalForm" @ok="clearTableSelectedRow" />
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import ShipmentGoodsBillModal from './modules/ShipmentGoodsBillModal'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import '@/assets/less/TableExpand.less'
import pick from 'lodash.pick'
import { downloadFileZip as downloadFile } from '@/api/manage'

export default {
  name: 'ShipmentGoodsBillList',
  mixins: [JeecgListMixin],
  components: {
    JDictSelectTag,
    ShipmentGoodsBillModal,
  },
  watch: {
    listData: {
      immediate: true,
      handler(val) {
        if (val) {
          let row = {
            storeId: val[0].id,
            storeName: val[0].store_name,
          }
          this.popupCallback(row)
        }
      },
    },
  },
  data() {
    return {
      description: '出货单管理页面',
      form: this.$form.createForm(this),
      listData: null,
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '出货单编号',
          align: 'center',
          dataIndex: 'id',
        },
        {
          title: '仓库',
          align: 'center',
          dataIndex: 'storeName',
        },
        {
          title: '单据状态',
          align: 'center',
          dataIndex: 'state_dictText',
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/shipmentgoodsbill/shipmentGoodsBill/list',
        delete: '/shipmentgoodsbill/shipmentGoodsBill/delete',
        deleteBatch: '/shipmentgoodsbill/shipmentGoodsBill/deleteBatch',
        exportZipUrl: '/shipmentgoodsbill/shipmentGoodsBill/exportXls',
        importExcelUrl: 'shipmentgoodsbill/shipmentGoodsBill/importExcel',
      },
      dictOptions: {},
    }
  },
  created() {},
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    importZipUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.exportZipUrl}`
    },
  },

  methods: {
    initDictConfig() {},
    handleApp(r) {
      this.$refs.modalForm.edit(r)
      this.$refs.modalForm.title = '审核'
      this.$refs.modalForm.disableSubmit = true
    },
    popupCallback(row) {
      let type
      if (row.routeIds !== undefined) {
        type = 'routeIds'
      } else {
        type = 'storeId'
      }
      this.form.setFieldsValue(pick(row, 'routeNames', 'storeName'))
      this.queryParam[type] = row[type]
    },
    searchReset1() {
      this.form.setFieldsValue({ storeName: '', routeNames: '' })
      this.searchReset()
    },
    //下载出货word
    handleExportXlsBtn(fileName) {
      if (!fileName || typeof fileName != 'string') {
        fileName = '导出文件'
      }
      let param = { ...this.queryParam }
      if (this.selectedRowKeys && this.selectedRowKeys.length > 0) {
        for (let i = 0; i < this.selectionRows.length; i++) {
          const item = this.selectionRows[i]
          if (this.selectedRowKeys[0] == item.id && [1, 2].includes(item.state)) {
            param['id'] = this.selectedRowKeys[0]
          } else {
            this.$message.warning('出货单状态不可导出')
            return
          }
        }
      } else {
        this.$message.warning('请先选择要导出出货单')
        return
      }

      downloadFile(this.importZipUrl, fileName, param)
      this.loadData()
    },
    clearTableSelectedRow() {
      this.onClearSelected()
      this.modalFormOk()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>